home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
-
- files.c
-
- This file contains the file I/O code for the QuickDraw GX unaware
- sample, "Simple Sample."
-
- Additional info can be found in the related develop #19 article,
- "Adding QuickDraw GX Printing to QuickDraw Applications."
-
- Dave Hersey, Apple Developer Technical Support.
-
- ——————— Edit Trail ———————
-
- hatched: 1/22/94 - dmh
- cleaned up for 2nd draft of develop article: 3/10/94 - dmh
- cleaned up for final: 4/14/94 - dmh
-
- *********************************************************************/
-
- #include "Simple Sample.h"
-
-
- /************************************************************
- MyLoadDocument - This routine loads a previously saved
- document.
-
- *************************************************************/
-
- OSErr MyLoadDocument(MyDocumentPtr whichDocument)
- {
- OSErr err;
- short oldResFile, dataRefNum = -1, resRefNum = -1;
- StandardFileReply sfReply;
- SFTypeList myTypeList;
- WindowPtr curWindow;
- MyDocumentPtr windDoc;
- Boolean notAlreadyOpen;
-
- // Let the user select a document to open.
-
- myTypeList[0] = kMyDocType;
- StandardGetFile(nil, 1, myTypeList, &sfReply);
- require_action(sfReply.sfGood, UserHasCancelled, err = iPrAbort;);
-
- /*
- Make sure that we haven't already opened this document. If
- we have, just bring the old window forward.
- */
- nrequire_action(MyIsWindowAlreadyOpen(&sfReply.sfFile),
- DocIsAlreadyOpen, err = iPrAbort;);
-
- // Open the selected file's data fork and resource fork.
-
- err = FSpOpenDF(&sfReply.sfFile, fsRdWrPerm, &dataRefNum);
- nrequire(err, CouldNotOpenDataFork);
-
- resRefNum = HOpenResFile(sfReply.sfFile.vRefNum, sfReply.sfFile.parID,
- sfReply.sfFile.name, fsRdPerm);
-
- err = ResError();
- nrequire(err, CouldNotOpenResourceFork);
-
- /*
- If we're successful in opening the file, set our document's
- FSSpec info, its title, and its window's title.
- */
-
- oldResFile = CurResFile();
-
- BlockMove(&sfReply.sfFile, &whichDocument->documentFSSpec, sizeof(FSSpec));
- BlockMove(&sfReply.sfFile.name, whichDocument->documentTitle,
- (long) sfReply.sfFile.name[0] +1);
-
- SetWTitle(whichDocument->documentWindow, whichDocument->documentTitle);
- err = MyLoadPrintInfo(whichDocument, resRefNum);
-
- // Now load the data for our document's pages.
-
- whichDocument->numPages = MyLoadPageCount(resRefNum);
- whichDocument->curPage = 1;
-
- /*
- Place your application-specific code here to load
- other data associated with the document.
- .
- .
- .
- */
-
- MyAdjustMenus();
-
- // Close the data and resource forks of this document.
-
- UseResFile(oldResFile);
- CloseResFile(resRefNum);
-
- CouldNotOpenResourceFork:
- FSClose(dataRefNum);
-
- CouldNotOpenDataFork:
- DocIsAlreadyOpen:
- UserHasCancelled:
- return err;
- }
-
-
- /************************************************************
- MyFSLoadDocument - This routine opens a document and loads
- its previously saved print record. It's just like
- MyLoadDocument, but it opens the indicated file and
- doesn't present a file dialog.
-
- *************************************************************/
-
- OSErr MyFSLoadDocument(MyDocumentPtr whichDocument, FSSpec *docFSSpec, Boolean forPrinting)
- {
- OSErr err;
- short oldResFile, dataRefNum, resRefNum;
-
- /*
- Unless we're printing, make sure that we haven't already opened
- this document. If we have, just bring the old window forward.
- */
-
- require_action(forPrinting || !MyIsWindowAlreadyOpen(docFSSpec),
- DocIsAlreadyOpen, err = iPrAbort;);
-
- // Open the selected file's data fork and resource fork.
-
- err = FSpOpenDF(docFSSpec, fsRdWrPerm, &dataRefNum);
- nrequire(err, CouldNotOpenDataFork);
-
- resRefNum = HOpenResFile(docFSSpec->vRefNum, docFSSpec->parID, docFSSpec->name, fsRdPerm);
- err = ResError();
- nrequire(err, CouldNotOpenResourceFork);
-
- /*
- If we're successful in opening the file, set our document's
- FSSpec info and its window's title.
- */
-
- oldResFile = CurResFile();
-
- BlockMove(docFSSpec, &whichDocument->documentFSSpec, sizeof(FSSpec));
- BlockMove(docFSSpec->name, whichDocument->documentTitle, (long) docFSSpec->name[0] +1);
- SetWTitle(whichDocument->documentWindow, whichDocument->documentTitle);
-
- err = MyLoadPrintInfo(whichDocument, resRefNum);
-
- // Now load the data for our document's pages.
-
- whichDocument->numPages = MyLoadPageCount(resRefNum);
- whichDocument->curPage = 1;
-
- /*
- Place your application-specific code here to load
- other data associated with the document.
- .
- .
- .
- */
-
- MyAdjustMenus();
-
- // Close the data and resource forks of this document.
-
- UseResFile(oldResFile);
- CloseResFile(resRefNum);
-
- CouldNotOpenResourceFork:
- FSClose(dataRefNum);
-
- CouldNotOpenDataFork:
- DocIsAlreadyOpen:
- return err;
- }
-
-
- /************************************************************
- MyIsWindowAlreadyOpen - This routine compares a file spec
- with those for our currently opened documents. If we find
- a match, we bring that document's window forward and return
- true. Otherwise, we return false.
-
- *************************************************************/
-
- Boolean MyIsWindowAlreadyOpen(FSSpec *whichFSSpec)
- {
- WindowPtr curWindow;
- MyDocumentPtr curDocument;
- Boolean isAlreadyOpen = false;
-
- /*
- Make sure that we haven't already opened this document. If
- we have, just bring the old window forward and adjust our menus.
- */
- curWindow = FrontWindow();
-
- while (curWindow != nil)
- {
- if (((WindowPeek) curWindow)->windowKind == userKind)
- {
- curDocument = MyGetDocPtr(curWindow);
-
- isAlreadyOpen =
- ((curDocument->documentFSSpec.vRefNum == whichFSSpec->vRefNum) &&
- (curDocument->documentFSSpec.parID == whichFSSpec->parID) &&
- (IUEqualString(curDocument->documentFSSpec.name, whichFSSpec->name) == 0));
-
- if (isAlreadyOpen)
- {
- SelectWindow(curWindow);
- MyAdjustMenus();
- }
-
- nrequire(isAlreadyOpen, DocIsAlreadyOpen);
- }
-
- curWindow = (WindowPtr) ((WindowPeek) curWindow)->nextWindow;
- }
-
- DocIsAlreadyOpen:
- NoNeedToCheck:
- return isAlreadyOpen;
- }
-
-
- /************************************************************
- MySaveDocument - This routine saves a document and its
- corresponding print record to disk.
-
- *************************************************************/
-
- OSErr MySaveDocument(MyDocumentPtr whichDocument, Boolean doingSaveAs)
- {
- OSErr err = noErr;
- short dataRefNum, oldResFile, resRefNum;
- FSSpec *docFSSpec;
- StandardFileReply sfReply;
- FInfo docFInfo;
-
- oldResFile = CurResFile();
- docFSSpec = &whichDocument->documentFSSpec;
-
- /*
- If we're doing a "Save as…", display the StandardFile
- dialog and have the user select a place to save the file.
- */
-
- if (doingSaveAs)
- {
- StandardPutFile("\pSave document:", whichDocument->documentTitle, &sfReply);
- require(sfReply.sfGood, UserHasCancelled);
-
- /*
- If we're replacing an existing file, delete it. Create
- our new file and set its creator and type.
- */
- if (sfReply.sfReplacing)
- err = HDelete(sfReply.sfFile.vRefNum, sfReply.sfFile.parID, sfReply.sfFile.name);
-
- nrequire(err, CouldNotDeleteOldFile);
-
- HCreateResFile(sfReply.sfFile.vRefNum, sfReply.sfFile.parID, sfReply.sfFile.name);
- HGetFInfo(sfReply.sfFile.vRefNum, sfReply.sfFile.parID,
- sfReply.sfFile.name, &docFInfo);
-
- docFInfo.fdCreator = kMyDocCreator;
- docFInfo.fdType = kMyDocType;
-
- err = HSetFInfo(sfReply.sfFile.vRefNum, sfReply.sfFile.parID,
- sfReply.sfFile.name, &docFInfo);
-
- /*
- If we're successful in creating the file, set our document's
- FSSpec info, its title and its window's title.
- */
-
- if (err == noErr) err = ResError();
- nrequire(err, CouldNotSetFileInfo);
-
- BlockMove(&sfReply.sfFile, docFSSpec, sizeof(FSSpec));
- BlockMove(&sfReply.sfFile.name, whichDocument->documentTitle,
- (long) sfReply.sfFile.name[0] +1);
-
- SetWTitle(whichDocument->documentWindow, whichDocument->documentTitle);
- }
-
- // Open the file's data fork and resource fork.
-
- err = FSpOpenDF(docFSSpec, fsRdWrPerm, &dataRefNum);
- nrequire(err, CouldNotOpenDataFork);
-
- resRefNum = HOpenResFile(docFSSpec->vRefNum, docFSSpec->parID,
- docFSSpec->name, fsRdWrPerm);
- err = ResError();
- nrequire(err, CouldNotOpenResourceFork);
-
- err = MySavePrintInfo(whichDocument, resRefNum);
-
- // Now save the data for our document's pages.
-
- if (!err)
- err = MySavePageCount(whichDocument, resRefNum);
-
- /*
- Place your application-specific code here to save
- other data associated with the document.
- .
- .
- .
- */
-
-
- // Close the data and resource forks of this document.
-
- CloseResFile(resRefNum);
-
- CouldNotOpenResourceFork:
- FSClose(dataRefNum);
-
- CouldNotOpenDataFork:
- CouldNotSetFileInfo:
- CouldNotDeleteOldFile:
- UserHasCancelled:
- UseResFile(oldResFile);
- return err;
- }
-
-
- /************************************************************
- MySavePageCount - This routine saves a resource containing
- the number of pages in our document.
-
- *************************************************************/
-
- OSErr MySavePageCount(MyDocumentPtr whichDocument, short resRefNum)
- {
- OSErr err;
- Handle thePageCount, oldPageCount;
-
- UseResFile(resRefNum);
-
- // If there's an existing resource, delete it.
-
- thePageCount = NewHandle(sizeof(long));
- nrequire((err = MemError()), CouldNotCreateHandle);
-
- *(long *)*thePageCount = whichDocument->numPages;
- oldPageCount = Get1Resource(kMyPageCountType, kMyPageCountID);
-
- if (oldPageCount != nil)
- {
- RmveResource(oldPageCount);
- UpdateResFile(resRefNum);
- DisposHandle(oldPageCount);
- }
-
- // Add our new resource.
-
- AddResource(thePageCount, kMyPageCountType, kMyPageCountID, "\p");
- err = ResError();
- nrequire(err, CouldNotAddResource);
-
- WriteResource(thePageCount);
- UpdateResFile(resRefNum);
- ReleaseResource(thePageCount);
-
- CouldNotAddResource:
- CouldNotCreateHandle:
- return err;
- }
-
-
- /************************************************************
- MyLoadPageCount - This routine loads a resource containing
- the number of pages in our document, and returns the number
- of pages.
-
- *************************************************************/
-
- long MyLoadPageCount(short resRefNum)
- {
- Handle thePageCount;
- long numPages;
-
- UseResFile(resRefNum);
-
- // Get the page count. If we don't find a count, return 1.
-
- thePageCount = Get1Resource(kMyPageCountType, kMyPageCountID);
-
- if (thePageCount != nil)
- {
- numPages = *(long *)*thePageCount;
- ReleaseResource(thePageCount);
- }
- else
- numPages = 1;
-
- return numPages;
- }
-
-
- /************************************************************
- MySavePrintInfo - This routine saves our print record with
- the document, so that it can be used the next time the
- document is opened.
-
- *************************************************************/
-
- OSErr MySavePrintInfo(MyDocumentPtr whichDocument, short resRefNum)
- {
- OSErr err;
- Handle thePrintData, oldPrintData;
-
- UseResFile(resRefNum);
-
- // If there's an existing resource, delete it.
-
- thePrintData = (Handle) whichDocument->documentPrintHdl;
- oldPrintData = Get1Resource(kMyPrintRecType, kMyPrintRecID);
-
- if (oldPrintData != nil)
- {
- RmveResource(oldPrintData);
- UpdateResFile(resRefNum);
- DisposHandle(oldPrintData);
- }
-
- // Add our new resource.
-
- AddResource(thePrintData, kMyPrintRecType, kMyPrintRecID, "\p");
- err = ResError();
- nrequire(err, CouldNotAddResource);
-
- WriteResource(thePrintData);
- UpdateResFile(resRefNum);
- DetachResource(thePrintData);
-
- CouldNotAddResource:
- return err;
- }
-
-
- /************************************************************
- MyLoadPrintInfo - This routine loads our document's
- previously saved print record.
-
- *************************************************************/
-
- OSErr MyLoadPrintInfo(MyDocumentPtr whichDocument, short resRefNum)
- {
- THPrint savedPrintHdl;
-
- UseResFile(resRefNum);
-
- /*
- Try to load a previously saved print record. If we find one,
- dispose of the print handle that we allocated in our
- MyCreateDocument routine.
- */
- savedPrintHdl = (THPrint) Get1Resource(kMyPrintRecType, kMyPrintRecID);
-
- if (savedPrintHdl != nil)
- {
- DetachResource((Handle) savedPrintHdl);
- DisposHandle((Handle) whichDocument->documentPrintHdl);
- whichDocument->documentPrintHdl = savedPrintHdl;
- }
-
- return noErr;
- }
-